Ext.define("AM.controller.MainController",{
	extend:'Ext.app.Controller',
	init:function(){
		this.control({
			'deptTree':{
			itemdblclick:function(tree,record,item,index,e,options){
					if(record.data.leaf){
						var tabId=record.data.id;
						alert(tabId);
						var tabText=record.get('text');
						var mainTabPanel=Ext.getCmp('MainLayout').child('#center').getComponent(0);
						var tab=mainTabPanel.getComponent(tabId);
						if(!tab){
							var newTab = {
				                    title:record.data.text,
				                    id:tabId,
				                    layout: 'fit',
//				                    iconCls:record.data.iconCls,
//				                    items:[{
////				                    	title:'xxxxxxxx',
////				                    	layout: 'fit',
//			                           	xtype:'userlist'
//				                    }],
				                    closable:true
			                };
							var v='AM.view.List';
							if(tabId=='0202'){
								v='AM.view.Test';
							}
							tab=mainTabPanel.add(Ext.create(v,newTab));
//							alert(tab.getId());
//							alert(tab.getXType());
//							tab.getStore().load();
						}
						 mainTabPanel.setActiveTab(tab);
					}
				}
			},
			'mainlayout button[id=addDeptList]':{
				click:function(showBtn){
					var tree=showBtn.ownerCt.ownerCt.child("#westTree").child("#deptTree");
//					if(tree)
//					{
//						var rootNode=tree.getStore().getRootNode();
//							rootNode.appendChild({
//							id:'01',
//							text:'undefine',
//							leaf:false
//						});
//					}
				}
			},
			'mainlayout button[id=showDept]':{
				click:function(o){
					var viewPort=o.ownerCt.ownerCt;
					var centerGrid = viewPort.child("#center").getComponent(0);
//					centerGrid.removeAll();
//					alert(centerGrid.getXType( ) );
				}
			},
			'mainlayout button[id=addDept]':{
				click:function(o){
					//Ext.Msg.alert('alert','ok!');
					var newDept=Ext.create('Ext.form.Panel',{
						plain: true,
						frame: true,
				        border: 0,
				        bodyPadding: 5,
//				        url: 'save-form.php',
				        fieldDefaults: {
				            labelWidth: 60,
				            anchor: '100%',
				            labelAlign: 'left'
				        },
				        layout: {
				            type: 'vbox',
				            align: 'stretch'
				        },
				        items:[
				            {xtype:'textfield',fieldLabel:'部门名称',name:'deptName'},
				        ]
					});
					var win=Ext.create('AM.form.WindowForm',{
						title: '部门信息',
				        collapsible: true,
				        animCollapse: true,
//				        maximizable: true,
				        width: 600,
				        height: 400,
				        minWidth: 300,
				        minHeight: 200,
				        layout: 'fit',
				        items: newDept,
				        dockedItems: [{
				            xtype: 'toolbar',
				            dock: 'bottom',
				            ui: 'footer',
				            layout: {
				                pack: 'center'
				            },
				            items: [{
				                minWidth: 80,
				                text: '保存',
				                handler:function(){
				            		alert(newDept.getForm().findField("deptName").getValue());
				            	}
				            },{
				                minWidth: 80,
				                text: '取消',
				                handler:function(){
//				            		alert("cancel");
				            		this.ownerCt.ownerCt.destroy();
				            	}
				            }]
				        }]
					});
					win.show();
				}
			}
		});
	},
	views:[
	    'DeptTree',   
	    'TabPanel',
	    'List',
	    'MainLayout',
	    'Test'
    ],
    stores:[
        'DeptStore4Tree',    
    	'Users',
    	'Test'
    ],
    models:[
        'User',
        'DeptModel',
        'Tt'
	]
});